package com.project.filter;

import com.project.bean.UserBean;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 顾客过滤器
 */
public class CustomFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //从session中得到登陆用户信息
        UserBean user = (UserBean)request.getSession().
                getAttribute("loginUser");
        if(user == null){
            response.sendRedirect("/html/login.html?errorInfo=noland");
        }
        else if(user.getPower().equals("顾客")){
            filterChain.doFilter(request,response);
        }
        else{
            response.sendRedirect("/html/login.html?errorInfo=nograde");
        }

    }
}
